package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	//查询所有分类：三级分类
	/*
	[
	{
		一级分类id：  1
		一级分类名称：小说
		children: [
			{
				二级分类id：  11
				二级分类名称：三国演义
				children:[
					{
						三级分类id：111
						三级分类名称：桃园三结义
					}
				]
			}
		]
	}
]
	 */
	@Override
	public List<JSONObject> getBaseCategoryList() {
		//新建一个集合，创建集合对象，存放最终数据
		List<JSONObject> finalList = new ArrayList<>();
		//查询所有分类(包含一级，二级，三级，使用view查询)，返回list集合
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		//2 把查询所有分类list集合封装成要求的数据格式
		//封装一级分类，根据一级分类id进行分组操作
		//map的key就是一级分类的id
		//map的value就是每组数据的集合
		Map<Long, List<BaseCategoryView>> map1 = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//遍历集合map1
		map1.forEach((key, value)->{
			Long cateGory1Id = key;
			List<BaseCategoryView> cateGory1List = value;
			String category1Name = cateGory1List.get(0).getCategory1Name();
			JSONObject category1 = new JSONObject();
			category1.put("categoryId",cateGory1Id);
			category1.put("categoryName",category1Name);

			//一级分类中有二级分类
			Map<Long, List<BaseCategoryView>> map2 = cateGory1List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			List<JSONObject> category2Child = new ArrayList<>();
			//map2里面的key就是二级分类的id，value就是每组数据的集合
			map2.forEach((key2,value2)->{
				Long category2Id = key2;
				List<BaseCategoryView> category2List = value2;
				JSONObject category2 = new JSONObject();
				category2.put("categoryId",category2Id);
				String category2Name = category2List.get(0).getCategory2Name();
				category2.put("categoryName",category2Name);

				//三级分类，没有重复的就直接写了
				//封装每个二级三级数据  category2List
				//List<BaseCategoryView>  -- List<JSONObject>
				List<JSONObject> category3List = category2List.stream().map(
						(baseCategoryView) -> {
							Long category3Id = baseCategoryView.getCategory3Id();
							String category3Name = baseCategoryView.getCategory3Name();
							JSONObject category3 = new JSONObject();
							category3.put("categoryId", category3Id);
							category3.put("categoryName", category3Name);
							return category3;
						}).collect(Collectors.toList());

				category2.put("categoryChild",category3List);
				category2Child.add(category2);
			});
			//把二级集合放到一级里面
			category1.put("categoryChild",category2Child);
			//把每个封装好的一级分类放到最终list集合里面finalList
			finalList.add(category1);
		});
		return finalList;
	}

	//根据一级分类id查询标签属性和属性值
	@Override
	public List<BaseAttribute> findAttribute(Long id) {
		return 	baseAttributeMapper.selectBaseAttribute(id);

	}

	//根据三级分类id查询所有分类数据
	@Override
	public BaseCategoryView getCatReegoryView(Long category3Id) {

		BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectById(category3Id);
		return baseCategoryView;
	}

	//根据一级分类id获取三级分类数据
	@Override
	public List<BaseCategory3> getCategory3IdByCategory1Id(Long category1Id) {
		LambdaQueryWrapper<BaseCategoryView> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(BaseCategoryView::getCategory1Id,category1Id);
		wrapper.last(" limit 7");
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(wrapper);
		List<BaseCategory3> baseCategory3List = baseCategoryViewList.stream().map(baseCategoryView -> {
			Long category3Id = baseCategoryView.getCategory3Id();
			BaseCategory3 baseCategory3 = baseCategory3Mapper.selectById(category3Id);
			return baseCategory3;
		}).collect(Collectors.toList());
		return baseCategory3List;
	}

	//根据一级分类id获取全部数据
	@Override
	public JSONObject getBaseCategoryList1(Long category1Id) {

		//1 封装一级分类
		// 根据一级分类id获取一级分类数据，
		BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(category1Id);
		//封装到JSONObject
		JSONObject category1 = new JSONObject();
		category1.put("categoryId",category1Id);
		category1.put("categoryName",baseCategory1.getName());

		LambdaQueryWrapper<BaseCategoryView> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(BaseCategoryView::getCategory1Id,category1Id);
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(wrapper);

		//二级分类的key是二级id，value是其他数据

		List<JSONObject> category2Child = new ArrayList<>();
		Map<Long, List<BaseCategoryView>> listMap2 = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		//遍历listMap2集合
		for (Map.Entry<Long,List<BaseCategoryView>> entry2 : listMap2.entrySet()){
			Long category2Id = entry2.getKey();
			List<BaseCategoryView> baseCategoryViews = entry2.getValue();
			JSONObject category2 = new JSONObject();
			String category2Name = baseCategoryViews.get(0).getCategory2Name();
			category2.put("categoryId",category2Id);
			category2.put("categoryName",category2Name);

			//封装三级分类
			List<JSONObject> category3Child = new ArrayList<>();
			baseCategoryViews.stream().forEach(baseCategoryView -> {

				Long category3Id = baseCategoryView.getCategory3Id();
				String category3Name = baseCategoryView.getCategory3Name();
				JSONObject category3 = new JSONObject();
				category3.put("categoryId",category3Id);
				category3.put("categoryName",category3Name);
				category3Child.add(category3);
			});

			category2.put("categoryChild",category3Child);

			category2Child.add(category2);
		}

		//把二级分类放到一级分类里面
		category1.put("categoryChild",category2Child);

		return category1;
	}

	//查询所有一级分类
	@Override
	public List<BaseCategory1> getCategory1() {
		List<BaseCategory1> baseCategory1List = baseCategory1Mapper.selectList(null);

		return baseCategory1List;
	}
}
